﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLLFactory;
using DataMode;
using IBLL;

namespace Face.Web.VideoPage
{
    public partial class VideoAlbumAdd : System.Web.UI.Page
    {
        private static IBLL.IAlbum bll = BLLFactory.DataAccess.GetAlbum();
        public string ThisUserName;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] == null)
                Response.Redirect("/login_and_signup/login.aspx");
            ThisUserName = Session["user"].ToString();
            UploadStatusLabel.Text = "";
        }

        protected void Button_Sumbition(object sender, EventArgs e)
        {
            try
            {
                if (this.albumPicture.HasFile)
                {
                    if (albumPicture.FileContent.Length > 1024 * 1024 * 5)//判断文件大小
                    {
                        UploadStatusLabel.Text = "上传图片过大，请选择5M以下图片上传。";
                    }
                    else
                    {
                        string type = albumPicture.FileName.Substring(albumPicture.FileName.LastIndexOf('.') + 1);
                        if (type == "jpg" || type == "gif" || type == "bmp" || type == "png")//判断文件类型
                        {
                            string savePath = SaveFile(albumPicture.PostedFile);//保存文件
                        }
                        else
                        {
                            UploadStatusLabel.Text = "请选择图片格式文件。";
                        }
                    }
                }
                else
                {
                    UploadStatusLabel.Text = "请选择上传图片。";
                }
                

            }
            catch (Exception ex)
            {
                this.UploadStatusLabel.Text = ex.Message;

            }

            albumDescription.Text = " ";
            albumName.Text = " ";

            //Response.Write("<script language='javascript'>window.location='../VideoPage/MyVideoAlbumsManage.aspx'</script>");
        }

        private string SaveFile(HttpPostedFile file)
        {
            string savePath = Request.PhysicalApplicationPath + @"\albumPicture\";
            string fileName = albumPicture.FileName;
            string pathToCheck = savePath + fileName;
            string tempfileName = "";
     
            if (System.IO.File.Exists(pathToCheck))//判断是否有同名文件存在
            {
                int counter = 2;
                while (System.IO.File.Exists(pathToCheck))
                {
                    tempfileName = counter.ToString() + "_" + fileName;
                    pathToCheck = savePath + tempfileName;
                    counter++;
                }

                fileName = tempfileName;
                UploadStatusLabel.Text = "有同名图片存在，" +
                    "上传图片被另存为 " + fileName;
            }
            else
            {
                UploadStatusLabel.Text = "图片上传成功。";
            }
            savePath += fileName;
            albumPicture.SaveAs(savePath);//保存文件


            video_album album = new video_album();

            album.album_create_time = DateTime.Now;
            album.album_description = albumDescription.Text;
            album.album_name = albumName.Text;
            album.album_path = @"\albumPicture\"+fileName;
            album.album_user = ThisUserName;

            bll.InsertAlbum(album);

            

            return fileName;

        }

        protected void btn_logout(object sender, EventArgs e)
        {
            Session.Remove("user");
            Response.Redirect("/login_and_signup/login.aspx");
        }
    }
}